package com.zxj.system.manager;

import com.zxj.common.core.contains.CacheConstants;
import com.zxj.common.redis.service.RedisService;
import com.zxj.system.domain.exam.entity.Exam;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

@Component
@RequiredArgsConstructor
public class ExamCacheManager {

    private final RedisService redisService;

    /**
     * 将竞赛信息加入缓存
     * @param exam 竞赛信息
     */
    public void addCache(Exam exam) {
        redisService.leftPushForList(getExamListKey(), exam.getExamId());
        redisService.setCacheObject(getDetailKey(exam.getExamId()), exam);
    }

    /**
     * 删除竞赛缓存信息
     * @param examId 竞赛id
     */
    public void deleteCache(Long examId) {
        redisService.removeForList(getExamListKey(), examId);
        redisService.deleteObject(getDetailKey(examId));
    }

    /**
     * 获取竞赛未完赛列表缓存key
     * @return 缓存key
     */
    private String getExamListKey() {
        return CacheConstants.EXAM_UNFINISHED_LIST;
    }

    /**
     * 获取竞赛详情缓存key
     * @param examId 竞赛id
     * @return 缓存key e:q:l:${examId}
     */
    private String getDetailKey(Long examId) {
        return CacheConstants.EXAM_DETAIL + examId;
    }

}
